package com.cat.monotonicStack;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/next-greater-element-ii/description/
 * @create 2025/8/12 21:24
 * @since JDK17
 */

public class Solution01 {
    static int N = ((int) (1e4 + 10)), top;
    static int[] stack = new int[N];

    public int[] nextGreaterElements(int[] nums) {
        int n = nums.length, cnt = 0, m = 2 * n - 1, r;
        int[] ans = new int[n];
        top = 0;
        for (int i = 0; i < m; i++) {
            while (top > 0 && nums[stack[top - 1]] < nums[i % n]) {
                ans[stack[--top]] = nums[i % n];
            }
            if (i < n) {
                stack[top++] = i;
            }
        }
        while (top > 0) {
            ans[stack[--top]] = -1;
        }
        return ans;
    }
}
